package com.ming.sportdemo.controller;

import com.alibaba.fastjson.JSON;
import com.auth0.jwt.JWT;
import com.ming.sportdemo.bean.User;
import com.ming.sportdemo.common.Result;
import com.ming.sportdemo.dao.UserDao;
import com.ming.sportdemo.filter.TokenService;
import com.ming.sportdemo.annotation.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.net.Inet4Address;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

@RestController
public class LoginController {
    @Autowired
    UserDao userDao;
    @Autowired
    TokenService tokenService;

    @RequestMapping("/valid")
    public Result Validate(@RequestParam("token") String token){
        String userId = JWT.decode(token).getAudience().get(0);
        User user = userDao.selectById(Integer.parseInt(userId));
        if(user!=null){
            Map<String,Object> res = new HashMap<>();
            res.put("username",user.getUsername());
            return Result.succ(res);
        }
        return Result.fail("验证错误");
    }
    @Log
    @RequestMapping("/login")
    public String login(@RequestBody User user){
        String flag="error";
        User us = userDao.getUserByMessage(user.getUsername(),user.getPassword());
        HashMap<String, Object> res = new HashMap<>();
        if(us!=null){
            String role = us.getRole();
            if(role.equals("管理员")||role.equals("超级管理员")){
                flag="ok";
                String token  = tokenService.getToken(us);
                res.put("token",token);
                res.put("flag",flag);
                res.put("user",us);
                res.put("role",role);
            }else {
                return null;
            }

        }

        String res_json = JSON.toJSONString(res);
        return res_json;
    }
    @RequestMapping("/clientLogin")
    public String clientLogin(@RequestBody User user, HttpServletRequest request) throws UnknownHostException {
        String flag="error";
        Inet4Address address = (Inet4Address) Inet4Address.getLocalHost();
        String hostAddress = address.getHostAddress();
        System.out.println(hostAddress);
        User us = userDao.getUserByMessage(user.getUsername(),user.getPassword());
        HashMap<String, Object> res = new HashMap<>();
        if(us!=null){
            String role = us.getRole();
            boolean state = us.getState();
            if(role.equals("普通用户")&&state){
                flag="ok";
                String token  = tokenService.getToken(us);
                res.put("token",token);
                res.put("flag",flag);
                res.put("userId",us.getId());
                res.put("role",role);
            }else {
                return null;
            }

        }

        String res_json = JSON.toJSONString(res);
        return res_json;
    }

}
